﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Linq;
using System.Xml.Linq;
using System.IO.IsolatedStorage;

namespace Holidays
{
    public partial class MainPage : PhoneApplicationPage
    {
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        bool newPageInstanse = false;
        XDocument Xdoc = new XDocument();
        save s = new save();
        
        public MainPage()
        {
            InitializeComponent();
            lstpicMonthNames.SelectionChanged += new SelectionChangedEventHandler(lstpicMonthNames_SelectionChanged);
            newPageInstanse = true;
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (newPageInstanse)
            {

                LoadMonthsInListPicker();
            }
            newPageInstanse = false;
        }
        private void LoadMonthsInListPicker()
        {
            List<Months> objMonthsList = new List<Months>();
            objMonthsList.Add(new Months() { MonthId = 1, MonthName = "January" });
            objMonthsList.Add(new Months() { MonthId = 2, MonthName = "Febraury" });
            objMonthsList.Add(new Months() { MonthId = 3, MonthName = "March" });
            objMonthsList.Add(new Months() { MonthId = 4, MonthName = "April" });
            objMonthsList.Add(new Months() { MonthId = 5, MonthName = "May" });
            objMonthsList.Add(new Months() { MonthId = 6, MonthName = "June" });
            objMonthsList.Add(new Months() { MonthId = 7, MonthName = "July" });
            objMonthsList.Add(new Months() { MonthId = 8, MonthName = "August" });
            objMonthsList.Add(new Months() { MonthId = 9, MonthName = "September" });
            objMonthsList.Add(new Months() { MonthId = 10, MonthName = "October" });
            objMonthsList.Add(new Months() { MonthId = 11, MonthName = "November" });
            objMonthsList.Add(new Months() { MonthId = 12, MonthName = "December" });
            lstpicMonthNames.ItemsSource = objMonthsList;
            lstpicMonthNames.SelectedIndex = DateTime.Now.Month - 1;
        }
        void lstpicMonthNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<Holidayprop> Hol = new List<Holidayprop>();
            string MnthId = Convert.ToString((lstpicMonthNames.SelectedItem as Months).MonthId);
            
                    XDocument xdoc = null;
                    xdoc = XDocument.Load("SampleData/Holiday.xml");
                    var holDetails = from i in xdoc.Descendants("Holiday")
                                      select i;
                    foreach (var hold in holDetails)
                    {
                        string dat = hold.Element("date").Value.Substring(0, hold.Element("date").Value.IndexOf("/"));
                        if (dat == MnthId)
                        {
                            Holidayprop h = new Holidayprop();
                            h.HolName = hold.Element("name").Value;
                            Hol.Add(s.LoadHolidays(hold.Attribute("id").Value.ToString(), MnthId));
                            Hol.Add(h);
                           
                        }
                        
                    }
                    lbxHolidays.ItemsSource = Hol;

            }
        private void lbxHolidays_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbxHolidays.SelectedIndex == -1)
                return;
            string id = (lbxHolidays.SelectedItem as Holidayprop).HolId;
            bool check = s.checkId(id);
            if (check)
            {
            }
            else
            {
                NavigationService.Navigate(new Uri("/Views/Places.xaml?holId=" + id, UriKind.Relative));

            }
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isoStore.FileExists("Holidays/HolidayPlans.xml"))
            {
                NavigationService.Navigate(new Uri("/Views/Details.xaml", UriKind.Relative));
            }
           
        }

       
    }
}